package logic

import (
	"context"
	"fmt"
	"furni-link/furnirpc/furnirpc"
	"furni-link/furnirpc/internal/svc"
	"furni-link/model/mysql"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type FurnShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurnShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurnShowLogic {
	return &FurnShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FurnShowLogic) FurnShow(in *furnirpc.FurnShowRequest) (*furnirpc.FurnShowResponse, error) {
	// todo: add your logic here and delete this line

	var furn mysql.Furn
	mysql.DB.Debug().Where("id = ?", in.FurnId).Find(&furn)

	if furn.ID == 0 {
		return nil, fmt.Errorf("当前家具不存在")
	}

	return &furnirpc.FurnShowResponse{
		FurnName:  furn.FurnName,
		Price:     float32(furn.Price),
		Num:       int64(furn.Num),
		ClassId:   int64(furn.ClassId),
		BrandId:   int64(furn.BrandId),
		FurnId:    int64(furn.ID),
		CreatedAt: furn.CreatedAt.Format(time.DateTime),
	}, nil
}
